package com.kaikai.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaikai.edu.entity.EduCourse;
import com.kaikai.edu.entity.EduCourseDescription;
import com.kaikai.edu.entity.frontvo.CourseFrontQueryInfo;
import com.kaikai.edu.entity.frontvo.CourseFrontVo;
import com.kaikai.edu.entity.vo.CourseInfoVo;
import com.kaikai.edu.entity.vo.CoursePublishVo;
import com.kaikai.edu.mapper.EduCourseMapper;
import com.kaikai.edu.service.EduChapterService;
import com.kaikai.edu.service.EduCourseDescriptionService;
import com.kaikai.edu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kaikai.edu.service.EduVideoService;
import com.kaikai.servicebase.exceptionhandler.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author yujiankai
 * @since 2021-05-14
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseMapper courseMapper;
    @Autowired
    private EduCourseDescriptionService descriptionService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse=new EduCourse();
        //添加课程信息
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int insert = courseMapper.insert(eduCourse);
        if (insert==0){
            throw new MyException(20001,"添加失败");
        }
        //添加描述信息
        EduCourseDescription description=new EduCourseDescription();
        description.setId(eduCourse.getId());
        description.setDescription(courseInfoVo.getDescription());
        descriptionService.save(description);
        return eduCourse.getId();
    }

    @Override
    public CourseInfoVo getCourseInfoById(String courseId) {

        CourseInfoVo courseInfoVo=new CourseInfoVo();
        EduCourse eduCourse = courseMapper.selectById(courseId);
        //查询用户是否存在
     if (eduCourse==null){
        throw new MyException(20001,"无此用户,请先添加");
        }
        BeanUtils.copyProperties(eduCourse, courseInfoVo);

        EduCourseDescription description = descriptionService.getById(courseId);
        courseInfoVo.setDescription(description.getDescription());

        return courseInfoVo;
    }


    @Override
    public void updateCourseInfo(CourseInfoVo form) {
        EduCourse course=new EduCourse();
        EduCourseDescription description=new EduCourseDescription();
        BeanUtils.copyProperties(form,course);
        BeanUtils.copyProperties(form, description);
        boolean b = this.updateById(course);

        boolean a = descriptionService.updateById(description);
        if((b=false)||(a=false)){
            throw new MyException(20001,"添加失败");
        }
    }

    @Override
    public CoursePublishVo getCoursePublishInfo(String courseId) {
        CoursePublishVo publishInfo = courseMapper.getCoursePublishInfo(courseId);
        return publishInfo;
    }

    @Override
    @Transactional
    public Boolean deleteCourseById(String courseId) {

        try {
            //1.删除小节
            eduVideoService.deleteCourseByCourseId(courseId);
            //删除章节
            eduChapterService.deleteChapterByCourseId(courseId);

            //3.删除描述
            descriptionService.removeById(courseId);
            //4.删除课程
            int i = courseMapper.deleteById(courseId);
            return i > 0;
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new MyException(20001,"删除失败");
        }
    }
    //获得首页八条课程信息
    @Cacheable(value = "course",key = "'8'")
    @Override
    public List<EduCourse> list8_courses() {
        QueryWrapper<EduCourse> courseWrapper=new QueryWrapper<>();
        courseWrapper.orderByDesc("id");
        courseWrapper.last("limit 8");
        List<EduCourse> courses = courseMapper.selectList(courseWrapper);
        return courses;
    }

    @Override
    public Map<String, Object> pageListCourse(Page<EduCourse> coursePage, CourseFrontQueryInfo queryInfo) {
       QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
       if(!StringUtils.isEmpty(queryInfo.getSubjectParentId())){
           wrapper.eq("subject_parent_id",queryInfo.getSubjectParentId());
       }
       if(!StringUtils.isEmpty(queryInfo.getSubjectId())){
           wrapper.eq("subject_id", queryInfo.getSubjectId());
       }

       if(!StringUtils.isEmpty(queryInfo.getPriceSort())){
           wrapper.orderByDesc(queryInfo.getPriceSort());
       }
       if(!StringUtils.isEmpty(queryInfo.getGmtCreateSort())){
           wrapper.orderByDesc(queryInfo.getGmtCreateSort());
       }
       if(!StringUtils.isEmpty(queryInfo.getBuyCountSort())){
           wrapper.orderByDesc(queryInfo.getBuyCountSort());
       }
        this.page(coursePage, wrapper);


        long total = coursePage.getTotal();
        long current = coursePage.getCurrent();
        long size = coursePage.getSize();
        boolean next = coursePage.hasNext();
        boolean previous = coursePage.hasPrevious();
        List<EduCourse> courseList = coursePage.getRecords();

        Map<String,Object> map=new HashMap<>();

        map.put("total",total);
        map.put("current",current);
        map.put("size",size);
        map.put("next",next);
        map.put("previous",previous);

        map.put("courseList", courseList);
        return map;
    }

    @Override
    public CourseFrontVo getCourseWebInfo(String courseId) {
       CourseFrontVo vo= baseMapper.getCourseWebInfo(courseId);
       return vo;
    }
}
